package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import connection.Reader;

/**
 * Trida pro okno umoznujici chatovani
 * 
 * @author dove
 * 
 */
public class ChatWin extends InternalFrame {

	private static final long serialVersionUID = 1L;
	private JPanel pan;
	private JTextField in;
	private Hashtable<String, JTextArea> channels;
	private JTabbedPane tabbedPane;
	private Hashtable<Integer, Boolean> hasNewMessages = new Hashtable<Integer, Boolean>();

	/**
	 * Inicializace chat okna
	 * 
	 * @param name
	 *            Nazev okna v GUI
	 */
	public ChatWin(String name) {

		super(name);
		tabbedPane = new JTabbedPane();
		channels = new Hashtable<String, JTextArea>();
		pan = new JPanel();
		in = new JTextField();
		in.setPreferredSize(new Dimension(300, 20));
		in.setActionCommand("\n");
		ActionListener lst = new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int b = tabbedPane.getSelectedIndex();
				String c = tabbedPane.getTitleAt(b);
				if (c.equals("Shout")) {
					Reader.sendString("shout " + in.getText());
				} else if (c.equals("Game")) {
					Reader.sendString("say " + in.getText());
				} else {
					Reader.sendString("tell " + c + " " + in.getText());
				}
				addText("You: " + in.getText() + "\n", c);
				in.setText("");
			}
		};
		in.addActionListener(lst);
		pan.setLayout(new BorderLayout());
		addChannel("Shouts");
		addChannel("Game");

		pan.add(tabbedPane, BorderLayout.CENTER);
		pan.add(in, BorderLayout.SOUTH);
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent evt) {
				int sel = tabbedPane.getSelectedIndex();

				if (hasNewMessages.get(sel)) {
					String channelNumber = tabbedPane.getTitleAt(sel);
					tabbedPane.setTitleAt(sel, channelNumber.substring(0,
							channelNumber.length() - 3));
					hasNewMessages.put(sel, false);
				}

			}
		});
		this.add(pan);
		this.pack();
		this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

	}

	/**
	 * Pridani kanalu do chat okna
	 * 
	 * @param channelNumber
	 *            cislo kanalu
	 */
	public void addChannel(String channelNumber) {
		channels.put(channelNumber, new JTextArea(12, 57));
		tabbedPane.addTab(channelNumber, new JScrollPane(channels
				.get(channelNumber)));
		int index = tabbedPane.indexOfTab(channelNumber);
		hasNewMessages.put(index, false);
		updateUI();
	}

	/**
	 * Odebrani kanalu z chat okna
	 * 
	 * @param channelNumber
	 *            cislo kanalu
	 */
	public void removeChannel(String channelNumber) {
		tabbedPane.remove(channels.get(channelNumber));
		updateUI();
	}

	/**
	 * Pridava text do chat okna
	 * 
	 * @param str
	 *            text ktery chceme pridat
	 * @param channel
	 *            nazev kanalu do ktereho text pridat
	 */
	public void addText(String str, String channel) {
		channels.get(channel).append(str);
		channels.get(channel).setCaretPosition(
				channels.get(channel).getDocument().getLength());
		int index = tabbedPane.indexOfTab(channel);
		if (index == -1) {
			index = tabbedPane.indexOfTab(channel + "(*)");
		} else {
			index = tabbedPane.indexOfTab(channel);
		}
		if (hasNewMessages.get(index) == false) {
			if (tabbedPane.getSelectedIndex() != tabbedPane.indexOfTab(channel)) {
				tabbedPane.setTitleAt(tabbedPane.indexOfTab(channel), channel
						+ "(*)");
				hasNewMessages.put(index, true);
			}
		}
	}
}
